#ifndef __MEMORY_HEADER_
#define __MEMORY_HEADER_

struct Memory_E820_Formate
{
    unsigned int address1;
    unsigned int address2;
    unsigned int length1;
    unsigned int length2;
    unsigned int type;
};

//页表大小4KB，指针大小8BYTE，因此可容纳512个表项
#define PTRS_PER_PAGE 512
//代表内核的起始线性地址，该地址位于物理内存0除
#define PAGE_OFFSET ((unsigned long)0xffff800000000000)

//左移多少位打到对应的大小
#define PAGE_GDT_SHIFT 39
#define PAGE_1G_SHIFT 30
#define PAGE_2M_SHIFT 21
#define PAGE_4K_SHIFT 12

#define PAGE_2M_SIZE (1UL << PAGE_2M_SHIFT)
#define PAGE_4K_SIZE (1UL << PAGE_4K_SHIFT)

//&后屏蔽对应大小的位
#define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
#define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))

//将参数按照2MB的上边界对齐
#define PAGE_2M_ALIGN(addr) ((unsigned long)(addr) + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
#define PAGE_4K_ALIGN(addr) (((unsigned long)(addr) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
//将内核虚拟内存地址转换为物理地址
#define Virt_To_Phy(addr) ((unsigned long)(addr)-PAGE_OFFSET)
#define Phy_To_Virt(addr) ((unsigned long *)((unsigned long)(addr) + PAGE_OFFSET))

// __attribute__((packed))修饰该结构体不会生成对齐空间
struct E820
{
    unsigned long address;
    unsigned long length;
    unsigned int type;
} __attribute__((packed));

struct Global_Memory_Descriptor
{
    struct E820 e820[32];
    unsigned long e820_length;
};
extern struct Global_Memory_Descriptor memory_management_struct;

#endif